﻿using Autofac;
using EWC.Autofac.DependencyInjection;
using System.Linq;
using System;
using Microsoft.AspNetCore.Mvc;
using EWC.HIS.Application;
using EWC.HIS.Dapper;

namespace EWC.HIS.WebApi.AutofacDI
{
    /// <summary>
    /// 配置Autofac依赖注入服务
    /// </summary>
    public class AutofacRegisterModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {

            //获取所有控制器类型并使用属性注入
            Type[] controllersTypeAssembly = typeof(Startup).Assembly.GetExportedTypes()
                .Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();
            builder.RegisterTypes(controllersTypeAssembly).PropertiesAutowired(new AutowiredPropertySelector());


            // 批量注入
            builder.BatchAutowired(typeof(Startup).Assembly);
            builder.BatchAutowired(typeof(ApplicationModule).Assembly);
            builder.BatchAutowired(typeof(DapperModule).Assembly);
        }
    }
}
